package com.cainiao.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * @author shkstart
 * @create 2021-08-10 11:19
 * 使用配置类代替 web.xml
 */
@Configuration
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
    /**
     * 来指定 spring的配置类
     *
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    /**
     * 来设置 springMVC的配置类
     *
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    /**
     * 获取 dispatchServlet的一个映射路径 url-pattern
     *
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    @Override
    protected Filter[] getServletFilters() {
        //配置编码过滤器
        CharacterEncodingFilter characterEncodingFilter
                = new CharacterEncodingFilter("UTF-8", true, true);
        //配置处理请求方式 put delete过滤器 
        HiddenHttpMethodFilter hiddenHttpMethodFilter
                = new HiddenHttpMethodFilter();
        return new Filter[]{characterEncodingFilter, hiddenHttpMethodFilter};
    }
}
